cmake_minimum_required(VERSION 3.15)

message("============== Env Setup ==============")

if(CMP0074)
  cmake_policy(SET CMP0074 NEW)
endif()
if(CMP0167)
  cmake_policy(SET CMP0167 NEW)
  set(NEW_BOOST_FIND ON)
endif()

include(CheckIncludeFileCXX)

set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)


if("${CMAKE_BUILD_TYPE}" STREQUAL "")
  set(CMAKE_BUILD_TYPE "Debug")
  message("AUTO SET CMAKE_BUILD_TYPE : Debug")
endif()

string(TOUPPER "${CMAKE_BUILD_TYPE}" UPPER_CMAKE_BUILD_TYPE)
if("DEBUG" STREQUAL "${UPPER_CMAKE_BUILD_TYPE}")
set(DEBUG_MODE 1)
  message("Debug 模式")
endif()

# 系统信息
if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux")
  message("This is Linux")
  set(IS_LINUX TRUE)
  set(PLATFORM_NAME "Linux")
  add_definitions(-DIS_LINUX)
elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Android")
  message("This is Android")
elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Windows")
  message("This is Windows")
  set(IS_WINDOWS TRUE)
  add_definitions(-DIS_WINDOWS)
  set(PLATFORM_NAME "Windows")
endif()

if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
  message("this is aarch64 cpu")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
  message("this is x86_64 cpu")
endif()

# 检查系统位数
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  set(ARCH_NAME "x64")
  message("Target is 64 bits")
else()
  set(ARCH_NAME "x32")
  message("Target is 32 bits")
  message(FATAL_ERROR "Not support on 32 bit platform")
endif()

# 强制使用 C++20 标准
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
message(STATUS "使用C++20标准")

# 如果需要，可以添加特性定义
add_definitions("-DSU_SUPPORT_CXX20=1")

add_definitions("-DENABLE_SILLY_LOG")
if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux")

  set(CMAKE_THREAD_LIBS_INIT "-lpthread")
  set(CMAKE_HAVE_THREADS_LIBRARY 1)
  set(CMAKE_USE_WIN32_THREADS_INIT 0)
  set(CMAKE_USE_PTHREADS_INIT 1)
  set(THREADS_PREFER_PTHREAD_FLAG ON)

  set(CMAKE_CXX_FLAGS
      "${CMAKE_CXX_FLAGS} -std=c++17 -lpthread -fPIC -Wall -fopenmp -Wno-unused-function -Wno-unused-variable -lodbc"
  )
  set(STATIC_LIB_SUFFIX "a")
  set(DYNAMIC_LIB_SUFFIX "so")
elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Windows")
  message("This is Windows")
  set(IS_WINDOWS TRUE)
  add_definitions(-DIS_WINDOWS)
  set(PLATFORM_NAME "Windows")
  
  # 添加NTSTATUS定义来解决ElaWidgetTools的编译错误
  add_definitions(-DNTSTATUS=LONG)
  
  # 确保必要的Windows头文件会被包含
  add_definitions(-D_WIN32_WINNT=0x0A00)
  
  add_definitions("-DIS_WIN32")
  add_compile_options(/wd4819 /wd4005 /wd4834 /wd4996 /utf-8 /openmp)
  set(STATIC_LIB_SUFFIX "lib")
  set(DYNAMIC_LIB_SUFFIX "dll")


if(MSVC)

    # 添加通用编译选项
    add_compile_options("/utf-8")
    add_definitions(
        -DWIN32_LEAN_AND_MEAN
        -DNOMINMAX
        -D_CRT_SECURE_NO_WARNINGS
    )

    # 设置链接器栈大小
    set(CMAKE_EXE_LINKER_FLAGS "")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:268435456")

    set(CMAKE_SHARED_LINKER_FLAGS "")
    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /STACK:268435456")

    set(CMAKE_CXX_FLAGS_DEBUG "")
    set(CMAKE_CXX_FLAGS_DEBUG "/D_DEBUG /MDd /Zi /Ob0 /Od /RTC1 /Gy /EHsc /bigobj")

    set(CMAKE_CXX_FLAGS_MINSIZEREL "")
    set(CMAKE_CXX_FLAGS_MINSIZEREL "/MD /Zi /O1 /Ob2 /Oi /Os /D NDEBUG /GS- /bigobj")
    
    # RelWithDebInfo 配置
    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "")
    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MD /Zi /O2 /Ob1 /D NDEBUG /bigobj")
    
    # Release 配置
    set(CMAKE_CXX_FLAGS_RELEASE "")
    set(CMAKE_CXX_FLAGS_RELEASE "/MD /Zi /O2 /Ob1 /D NDEBUG /bigobj")
    
    # 为 Release 配置添加额外的链接器选项
    set(CMAKE_EXE_LINKER_FLAGS_RELEASE "")
    set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG")

endif(MSVC)

endif() # CMAKE_HOST_SYSTEM_NAME MATCHES = Windows

# 设置输出路径
set(EXECUTABLE_OUTPUT_PATH
    "${CMAKE_SOURCE_DIR}/Bin/${PLATFORM_NAME}/${ARCH_NAME}_${CMAKE_BUILD_TYPE}")
set(LIBRARY_OUTPUT_PATH "${EXECUTABLE_OUTPUT_PATH}")
message (STATUS "Executable Output Path: ${EXECUTABLE_OUTPUT_PATH}")